package server.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import common.IClient;

import server.control.ServerControl;
import entity.Constants;

public class ClientManagerPanel extends JPanel implements Runnable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2170912538008001659L;
	
	//array contain client object that include information
	public ServerControl serverCtrl;
	public ArrayList<Row> rows =  new ArrayList<Row>();
	private JLabel header;
	
	public ClientManagerPanel(ServerControl server) {
		
		serverCtrl = server;
		
		//set dimension of panel
		this.setPreferredSize(new Dimension(Constants.PANEL_CM_WIDTH, Constants.PANEL_CM_HEIGHT));
		this.setLayout(new FlowLayout(FlowLayout.LEADING));
		this.setVisible(true);
		
		initComponents();
	}
	
	private void initComponents() {
		setBorder(BorderFactory.createEtchedBorder());
		header = new JLabel("         Địa chỉ IP                    ID            Tình trạng                                Điều khiển                           ");
		header.setBackground(Color.GRAY);
		header.setBorder(BorderFactory.createEtchedBorder());
		
		header.setLocation(0, 20);
		this.add(header);
		
		//createRow();
	}
	
	public void recreatePreRow() {
		this.add(header);
		for(Row r : rows) {
			this.add(r);
		}
	}
	
	public void emptyRowList() {
		rows.removeAll(rows);
	}
	
	public void createNewRow() {
		Row row;
		if(serverCtrl.clients.size() == 0) {
			return;
		}
			
		try {
			
			for(int i = 0; i < serverCtrl.clients.size(); i++) {
				//String address = RemoteServer.getClientHost();
				row = new Row(	"clietn",
								serverCtrl.clients.get(i).getId(),
								"connected"
							  );
				
				row.serverCtrl = serverCtrl;
				rows.add(row);
			}
			
			//row.rows = rows;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public synchronized void removeRow(int id) {
		
		for(Row r : rows) {
//			System.out.println(r.lbId.getText()+ "  :  " +id);
			if(Integer.parseInt(r.lbId.getText()) == id) {
				rows.remove(r);
				//return;
			}
		}
		
		for(Row r : rows) {
			System.out.println(r.lbId.getText()+ "  :  " +id);
		}
	}

	@Override
	public void run() {

	}
}

class Row extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 4819175604253856837L;
	public Label lbName, lbId, lbStatus;
	private JButton btnConnect, btnDisconnect, btnShutdown;
	public ServerControl serverCtrl;
	public ArrayList<Row> rows;
	
	public Row(String name, int id, String status) {
		
		this.setPreferredSize(new Dimension(485, 45));
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		setBorder(BorderFactory.createBevelBorder(0, Color.DARK_GRAY, Color.LIGHT_GRAY));
		this.setVisible(true);
		
		 lbName = new Label(name);
		 lbId = new Label(Integer.toString(id));
		 lbStatus = new Label(status);
		 
		 Dimension dName = new Dimension(130, 20);
		 lbName.setPreferredSize(dName);
		 
		 Dimension dId = new Dimension(40, 20);
		 lbId.setPreferredSize(dId);
		 
		 Dimension dStatus = new Dimension(70, 20);
		 lbStatus.setPreferredSize(dStatus);
		 
		 this.add(lbName);
		 this.add(lbId);
		 this.add(lbStatus);
		 
		 createButton();
	}
	
	private void createButton() {
		
		Font font = new Font("Arial", Font.BOLD, 10);
		
		btnConnect = new JButton("connect");
		btnDisconnect = new JButton("disconnect");
		btnShutdown = new JButton("shutdown");
		
		btnConnect.setPreferredSize(new Dimension(70, 30));
		btnDisconnect.setPreferredSize(new Dimension(70, 30));
		btnShutdown.setPreferredSize(new Dimension(70, 30));
		
		btnConnect.setFont(font);
		btnDisconnect.setFont(font);
		btnShutdown.setFont(font);
		
		btnConnect.setMargin(new Insets(2, 2, 2, 2));
		btnDisconnect.setMargin(new Insets(2, 2, 2, 2));
		btnShutdown.setMargin(new Insets(2, 2, 2, 2));
		
		this.add(btnConnect);
		this.add(btnDisconnect);
		this.add(btnShutdown);
		
		setButtonEnable(false, true, true);
		
		btnConnect.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {

				setButtonEnable(false, true, true);
				lbStatus.setText("Connected");
				
				try {
					
					for(IClient client : serverCtrl.clients) {
						if(client.getId() == (Integer.parseInt(lbId.getText()))) {
							
							client.isConnected(true);
							return;
						}
					}
				} catch (NumberFormatException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		
		btnDisconnect.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				setButtonEnable(true, false, true);
				lbStatus.setText("Disconnected");
				
				try {
					
					for(IClient client : serverCtrl.clients) {
						if(client.getId() == (Integer.parseInt(lbId.getText()))) {
							
							client.isConnected(false);
							return;
							//serverCtrl.clients.get(Integer.parseInt(lbId.getText()) - 1).isConnected(false);
						}
					}
				} catch (NumberFormatException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		
		btnShutdown.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				/*loai bo client trong mang clients
				 * tat chuong trinh ben client
				 * loai bo row trong mang rows
				 * cap nhat lai panel
				 * 
				 */
				int id = Integer.parseInt(lbId.getText());
				
				for(IClient client : serverCtrl.clients) {
					try {
						if(client.getId() == id) {
							try  {
								client.shutdown();
							}
							catch(IOException io) {
								
							}
							serverCtrl.clients.remove(client);
							break;
						}
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
				
				serverCtrl.refreshCMPanel();
			}
		});
	}
	
	//set enable of buttons
	private void setButtonEnable(boolean con, boolean dis, boolean shut) {
		btnConnect.setEnabled(con);
		btnDisconnect.setEnabled(dis);
		btnShutdown.setEnabled(shut);
	}
}
